package com.luoling.readExcel;

import lombok.Data;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Description:
 * Copyright:   Copyright (c)2024
 * Company:
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2024-07-16 16:37:16
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2024-07-16     张李鑫                     1.0         1.0 Version
 */
@Data
public class Rule {
    private Integer index;

    public enum Comparison {
        EQUALS,
        GREATER_THAN,
        LESS_THAN,
        IN
    }

    private Comparison comparison;
    private String value;

    private Set<String> filterSet;

    public Rule() {
    }

    public Rule(Integer index, Comparison comparison, String value) {
        this.index = index;
        this.comparison = comparison;
        this.value = value;
    }

    public boolean filter(Map<Integer, String> data) {
        switch (comparison) {
            case EQUALS:
                return eq(data);
            case GREATER_THAN:
                return greaterThan(data);
            case LESS_THAN:
                return lessThan(data);

            case IN:
                return in(data);
            default:
                return true;
        }
    }

    private boolean in(Map<Integer, String> data) {
        if (this.filterSet == null) {
            String[] split = value.split("\n");
            this.filterSet = new HashSet<>(Arrays.asList(split));
        }
        return this.filterSet.contains(data.get(index));
    }

    private boolean lessThan(Map<Integer, String> data) {
        return true;
    }

    private boolean greaterThan(Map<Integer, String> data) {
        return true;
    }

    private boolean eq(Map<Integer, String> data) {
        return data.get(index).equals(value);
    }
}
